-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rock Paper Scissor by deso Initial Commit #4
base: main
Are you sure you want to change the base?
Conversation
const random = () => { | ||
return Math.floor(Math.random() * 3); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep the declaration before usage, for better understanding.
// const object = { | ||
// p1: 1, | ||
// p2: 2, | ||
// p3: 3, | ||
// p4: { | ||
// p5: 4, | ||
// }, | ||
// }; | ||
// console.log(document.getElementsByTagName("p")); | ||
|
||
// const prom = new Promise((res, rej) => { | ||
// setTimeout(() => { | ||
// rej("Working"); | ||
// }, 10000); | ||
// }); | ||
|
||
// console.log( | ||
// prom.then(() => console.log("done")).catch(() => console.log("rejected")) | ||
// ); | ||
|
||
fetch("https://portal.tycoonstats.com/api/demo") | ||
.then((response) => response.text()) | ||
.then((data) => console.log(data)); | ||
|
||
async function a() { | ||
let response = await fetch("https://portal.tycoonstats.com/api/demo"); | ||
let data = await response.text(); | ||
console.log(data); | ||
} | ||
|
||
a(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Make separate commit for different work.
- Double check your code before force pushing.
- Donot push unused / commented out code , make habit of checking the code with "git diff" before updating the commit.
const play = () => { | ||
rockBtn.addEventListener("click", () => { | ||
let computerChoice = random(); | ||
computerChoiceSpan.innerText = computersOptions[computerChoice]; | ||
humanChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand-back-fist fa-5x"></i>'; | ||
humanChoiceSpan.innerText = "Rock"; | ||
if (computerChoice === 0) { | ||
result.innerText = "Draw"; | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand-back-fist fa-5x"></i>'; | ||
} else if (computerChoice === 1) { | ||
result.innerText = "Computer Won!!!"; | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand fa-5x"></i>'; | ||
computerScore.innerText++; | ||
} else { | ||
result.innerText = "You Won!!!"; | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand-scissors fa-5x"></i>'; | ||
humanScore.innerText++; | ||
} | ||
}); | ||
paperBtn.addEventListener("click", () => { | ||
let computerChoice = random(); | ||
computerChoiceSpan.innerText = computersOptions[computerChoice]; | ||
humanChoiceIcon.innerHTML = '<i class="fa-solid fa-hand fa-5x"></i>'; | ||
humanChoiceSpan.innerText = "Paper"; | ||
if (computerChoice === 0) { | ||
result.innerText = "You Won!!!"; | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand-back-fist fa-5x"></i>'; | ||
humanScore.innerText++; | ||
} else if (computerChoice === 1) { | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand fa-5x"></i>'; | ||
result.innerText = "Draw"; | ||
} else { | ||
result.innerText = "Computer Won!!!"; | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand-scissors fa-5x"></i>'; | ||
computerScore.innerText++; | ||
} | ||
}); | ||
scissorBtn.addEventListener("click", () => { | ||
let computerChoice = random(); | ||
computerChoiceSpan.innerText = computersOptions[computerChoice]; | ||
humanChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand-scissors fa-5x"></i>'; | ||
humanChoiceSpan.innerText = "Scissor"; | ||
if (computerChoice === 0) { | ||
result.innerText = "Computer Won!!!"; | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand-back-fist fa-5x"></i>'; | ||
|
||
computerScore.innerText++; | ||
} else if (computerChoice === 1) { | ||
result.innerText = "You Won!!!"; | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand fa-5x"></i>'; | ||
humanScore.innerText++; | ||
} else { | ||
result.innerText = "Draw"; | ||
computerChoiceIcon.innerHTML = | ||
'<i class="fa-solid fa-hand-scissors fa-5x"></i>'; | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for this specific case we can add logic of querySelectorAll("botton"), and for each button click we can call a method that will update the scenario who win.
Re- write the code: using concept of:
querySelectorAll("botton"), use foreach, and onclick call one method that will update the state, also avoid using multiple if...else and repeating code, use ternary operator as user can win in three cases only
you_win = ( (you === "rock") && (opponent === "scissor") ||
(you === "paper") && (opponent === "rock") ||
(you === "scissor") && (opponent === "rock") ) // returns true for your win cases only.
so (you === opponent) ? "draw message" : you_win ? call a method for updating score and message for user : call method for updating score and message fro computer;
No description provided.